package top.szhome.framework.core.tools.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.Nullable;

/**
 * @author 唐加利
 * @date 2023/8/11
 **/
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
        SpringUtil.context = context;
    }

    /**
     * 获取指定类型的Bean
     * @param tClass    Bean类型
     * @param <T>       Bean类型
     * @return  返回Spring容器中指定类型的Bean
     */
    public static <T> T getBean(Class<T> tClass) {
        return context.getBean(tClass);
    }

}
